#include"RingQueue.hpp"
#include"Task.hpp"
#include"MyThread.hpp"
using namespace MyThread;
#include<vector>
#include<unistd.h>
using namespace std;
void Producercode(RingQueue<Task_t>*prq,string name)
{
    while(1)
    {
        sleep(1);
        prq->Enqueue(print);
        cout<<name<<" produce a task"<<endl;
    }

}
void Consumercode(RingQueue<Task_t>*prq,string name)
{
    sleep(10);
    while(1)
    {
        sleep(1);
        Task_t t;
        prq->Pop(&t);
        cout<<name<<" get a task...";
        t();
    }
}
void InitProducer(vector<Thread<RingQueue<Task_t>*>>&threads,int num,RingQueue<Task_t>*prq)
{
    for(int i=1;i<=num;i++)
    {
        string name="producer thread-"+to_string(i);
        threads.emplace_back(Producercode,prq,name);
    }

}
void InitConsumer(vector<Thread<RingQueue<Task_t>*>>&threads,int num,RingQueue<Task_t>*prq)
{
    for(int i=1;i<=num;i++)
    {
        string name="consumer thread-"+to_string(i);
        threads.emplace_back(Consumercode,prq,name);
    }
}
void StartAll(vector<Thread<RingQueue<Task_t>*>>&threads)
{
    for(auto&e:threads)
    e.start();
}
void WaitAll(vector<Thread<RingQueue<Task_t>*>>&threads)
{
    for(auto&e:threads)
    e.join();
}
int main()
{
    RingQueue<Task_t> rq(5);
    vector<Thread<RingQueue<Task_t>*>>threads;//要把环形队列指针传给各个线程
    InitProducer(threads,3,&rq);
    InitConsumer(threads,4,&rq);

    StartAll(threads);
    WaitAll(threads);



    return 0;
}